; ctgdemo.asm
; vic-20 version of the CTG "demo"

	.include "common.inc"
	.include "global.inc"
	.include "screen.inc"
	.include "graphic.inc"
	.include "text.inc"
	.include "timer.inc"

	.macpack cbm

	.export ctg_init
	.export ctg_run 
	
	.segment "DATA"
	
h_block:	.res 1

tx_demo:	scrcode "commodore treffen graz    "
	tx_len = * - tx_demo - 4
	txp_len = * - tx_demo

txi_demo:	scrcode "    commodore treffen graz"
	txi_len = * - txi_demo
	
cur_ch:		.res 1
cur_line:	.res 1
cur_cnt:	.res 1
cur_noise:	.res 1

	ptr = strptr
	
	.segment "CODE"
	
ctg_init:	; alloc char block
			lda #MAX_COLS				; block size
			ldy #1						; # rows
			jsr sg_alloc
			sty h_block
			; draw into block
			tya						
			jsr g_set_frame				; select block for graphic ops
			lda #0						; clear block
			jsr g_clr
			lda #GM_SET					; set paint mode
			jsr g_mode
			ldx #0						; move to upper left
			ldy #0 
			jsr g_moveto
			lda #0						; select system font 1
			jsr t_font
			lda #<tx_demo				; print first line
			ldx #>tx_demo
			ldy #tx_len
			jsr t_text
			ldx #0						; move to next line, indent 4
			ldy #8 
			jsr g_moveto
			ldx #MAX_WIDTH				; invert text area
			ldy #8
			jsr g_block
			lda #GM_CLEAR				; set paint mode
			jsr g_mode
			ldx #MAX_WIDTH - tx_len*8	; move to next line, indent 4
			ldy #8 
			jsr g_moveto
			lda #<tx_demo		; print second line
			ldx #>tx_demo
			ldy #tx_len
			jsr t_text
			
			rts			
			
			
ctg_run:	; fill screen with text
			lda h_block
			jsr sg_get_block
			sta cur_ch
			lda #0
			sta cur_line
			
@showloop:	lda #<VIDEO_BASE
			sta ptr
			lda #>VIDEO_BASE
			sta ptr+1
		
			jsr wait_refresh
			lda cur_ch
			
			sta @ch_adr+1 
			ldy cur_line
			ldx #MAX_ROWS

@drawloop:	cpy #MAX_COLS	
			bcs @skip
@ch_adr:	lda #0
			sta (ptr), y
@skip:		clc
			lda ptr
			adc #MAX_COLS
			sta ptr
			bcc @next
			inc ptr+1
@next:		dec @ch_adr+1
			dex
			beq @exit
			dey
			bpl @drawloop
			
@exit:		inc cur_line
			inc cur_ch
			lda cur_line			
			cmp #MAX_COLS+MAX_ROWS
			bcc @showloop

			; switch to 8x8 mode
			jsr wait_refresh
			ldx #0
			txa
@clrclr:	sta COLOR_BASE+$0000, x
			sta COLOR_BASE+$0100, x
			sta COLOR_BASE+$0200, x
			sta COLOR_BASE+$0300, x
			inx
			bne @clrclr
			lda #4*MAX_ROWS
			sta VIC_BASE+3
			lda #$C0
			sta VIC_BASE+5
			ldx #txp_len-1
@txploop:	lda tx_demo, x
			.repeat MAX_ROWS, I
			sta VIDEO_BASE + MAX_COLS*2*I, x 
			.endrep
			dex
			bpl @txploop

			ldx #txi_len-1
@txiloop:	lda txi_demo, x
			ora #128
			.repeat MAX_ROWS, I
			sta VIDEO_BASE + MAX_COLS*(2*I+1), x 
			.endrep
			dex
			bpl @txiloop

			jsr wait_refresh
			ldx #0
			lda #1
@setclr:	sta COLOR_BASE+$0000, x
			sta COLOR_BASE+$0100, x
			sta COLOR_BASE+$0200, x
			sta COLOR_BASE+$0300, x
			inx
			bne @setclr

			; animate colors
			lda #0
			sta cur_line
			lda #2
			sta cur_ch

@olp1:		sec
			lda #MAX_ROWS
			sbc cur_line
			sta cur_cnt
			
			lda #<(COLOR_BASE+(MAX_ROWS-1)*2*MAX_COLS)
			sta ptr
			lda #>(COLOR_BASE+(MAX_ROWS-1)*2*MAX_COLS)
			sta ptr+1
			
@ilp1:		jsr wait_refresh
			lda cur_ch
			ldy #MAX_COLS-1
@clrl:		sta (ptr), y
			dey
			bpl @clrl
			lda #1
			ldy #3*MAX_COLS-1
@clrw:		sta (ptr), y
			dey
			cpy #2*MAX_COLS
			bcs @clrw
			
			sec
			lda ptr
			sbc #2*MAX_COLS
			sta ptr
			lda ptr+1
			sbc #0
			sta ptr+1			
			dec cur_cnt
			bne @ilp1
			
			ldx cur_ch
			inx
			cpx #8
			bcc @clrok
			ldx #2
@clrok:		stx cur_ch
			
			inc cur_line
			lda cur_line
			cmp #MAX_ROWS
			bcc @olp1
			
			; copper
			lda #10						; sound on!
			sta VIC_BASE+14			
			lda #255
			sta VIC_BASE+12

			ldy #0
			sta cur_line
			jsr wait_refresh

@coplp:		lda #7
			sta cur_cnt
			ldy cur_line
			lda lintab, y
			tax
			lda clrtab, y
@l1sync:	cpx VIC_BASE+4
			bne @l1sync
			sta VIC_BASE+15
			iny
			lda clrtab, y
			inx
			dec cur_cnt
			bne @l1sync
			
			inx
			lda #8
@l2sync:	cpx VIC_BASE+4
			bne @l2sync
			sta VIC_BASE+15
			
			inc cur_line
			lda cur_line
			and #$01
			bne @coplp
			
			clc
			lda VIC_BASE+1
			adc #1
			cmp #160
			bcs @fin
			sta VIC_BASE+1
			
			dec VIC_BASE+12

			jmp @coplp
			
@fin:		lda #0
			sta VIC_BASE+12
			lda #200
			sta VIC_BASE+13
			
			ldx #15
@expl:		jsr wait_refresh
			jsr wait_refresh
			stx VIC_BASE+14
			dex
			bpl	@expl
			
			lda #0
			sta VIC_BASE+13
			sta VIC_BASE+14

@idle:		jmp @idle

	.segment "RODATA"
	
clrtab:		.repeat 37
			.byte $7F, $6E, $5D, $4C, $3B, $2A, $19
			.endrep	
lintab:		.incbin "linetab.bin"

